package cn.edu.sdjzu.xg2106.service;

import cn.edu.sdjzu.xg2106.dao.SchoolDao;
import cn.edu.sdjzu.xg2106.domain.School;
import cn.edu.sdjzu.xg2106.exception.BysjException;
import cn.edu.sdjzu.xg2106.util.Condition;
import cn.edu.sdjzu.xg2106.util.JdbcHelper;
import cn.edu.sdjzu.xg2106.util.Pagination;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class SchoolService {
    private static final SchoolDao schoolDao = SchoolDao.getInstance();
    public Pagination pagination;
    private static final SchoolService schoolService = new SchoolService();
    private SchoolService(){}

    public static SchoolService getInstance(){
        return schoolService;
    }

    public boolean delete(int id) throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        School school=schoolService.find(id);
        int countDepartmentsOfSchool=DepartmentService.getInstance().countAll(school);
        if(countDepartmentsOfSchool==0){
            return schoolService.delete(id);
        }else {
            throw new BysjException(school.getDescription()+"仍然有下属系，不能删除");
        }
    }
    public void add(School school)throws SQLException {
        Connection connection = JdbcHelper.getConn();
        schoolDao.add(school,connection);
    }
    public void update(School school) throws SQLException {
        schoolDao.update(school);
    }
    public School find(int id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        School school = schoolDao.find(id,connection);
        JdbcHelper.close(connection);
        return school;
    }





    public School find1(Integer id) throws SQLException {

        School desiredSchool = schoolDao.find1(id) ;
        return desiredSchool;
    }

    public Collection<School>findAll(List<Condition> conditionList, Pagination pagination)throws SQLException, BysjException {

        Connection connection = JdbcHelper.getConn();
        Collection<School> schools = schoolDao.findAll(connection,conditionList,pagination);
        JdbcHelper.close(connection);
        return schools;
    }




}
